
package gui;

import java.util.Collection;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import exceptions.UPException;
import model.Job;
import model.ListManager;

/**
 * VolunteerTableModel class.
 * 
 * @author Doug Hutchens
 * @version 1.0
 */

@SuppressWarnings("serial")
public class VolunteerTableModel extends AbstractTableModel  {
	
	/** The model to use. */
	
	private DefaultTableModel my_model;
	
	/** The collection of jobs. */
	
	private Collection<Job> my_jobs;
	
	/** Whether the model contains all jobs or not. */
	
	private boolean my_all;
	
	/** Instance of ListManager used to access items
	 *  needed for model.
	 */
	
	private ListManager my_lm;
	
	/**
	 * Constructor for VolunteerTableModel.
	 * 
	 * @param the_jobs collection to add to model.
	 * @param the_all whether collection contains all jobs.
	 * @param the_lm ListManager from frame.
	 */
	
	public VolunteerTableModel(Collection<Job> the_jobs,
			                   boolean the_all,
			                   ListManager the_lm) {
		my_jobs = the_jobs;
		my_all = the_all;
		my_lm = the_lm;
		my_model = new DefaultTableModel();
		initialize();
		
	}
	
	/**
	 * Gets the row count for the model.
	 */

	@Override
	public int getRowCount() {
		return my_model.getRowCount();
	}
	
	/**
	 * Gets the column count for the model.
	 */

	@Override
	public int getColumnCount() {
		
		return my_model.getColumnCount();
	}
	
	/**
	 * Gets the value at a specific row and column.
	 */

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		
		return my_model.getValueAt(rowIndex, columnIndex);
	}
	
	/**
	 * Gets the column name.
	 */
	
	public String getColumnName(int col) {
		return my_model.getColumnName(col);
	}

	/**
	 * gets column class.  This needs to stay
	 * with errors.  Please do not remove.
	 */
	
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int o) {
		return my_model.getValueAt(0,o).getClass();
	}
    
	/**
	 * Checks if a cell is editable or not.
	 */
	
	public boolean isCellEditable(int row, int col) {
		boolean result = false;
		
		if (getColumnName(13).equals("Volunteered")) {			
			if (getValueAt(row, col) instanceof Boolean) {			
				if (!(Boolean) getValueAt(row, col)) {
					result = true;
				}
			}
		}
		
		return result;
	}
	
	/**
	 * Sets the value at a specific row and column and
	 * fires the firetablecellupdated event.
	 */
	
	public void setValueAt(Object value, int row, int col) {
		my_model.setValueAt(value, row, col);			
	}
	
	/**
	 * Initializes the table model.
	 */
	
	private void initialize() {
		
		my_model.addColumn("Name");
		my_model.addColumn("Park");
		my_model.addColumn("Street");
		my_model.addColumn("City");
		my_model.addColumn("State");
		my_model.addColumn("Start Date");	
		my_model.addColumn("Start Time");
		my_model.addColumn("End Date");
		my_model.addColumn("End Time");
		my_model.addColumn("Heavy");
		my_model.addColumn("Medium");
		my_model.addColumn("Light");
		my_model.addColumn("Id");
		
		if (my_all) {
			my_model.addColumn("Volunteered");
		}	
		
		Object[] my_data = new Object[my_model.getColumnCount()];		
		
		for (Job j : my_jobs) {
			try {
				my_data[0] = j.getName();
				my_data[1] = my_lm.findPark(j.getParkId()).getName();
				my_data[2] = my_lm.findPark(j.getParkId()).getAddress().getStreet();
				my_data[3] = my_lm.findPark(j.getParkId()).getAddress().getCity();
				my_data[4] = my_lm.findPark(j.getParkId()).getAddress().getState();
				my_data[5] = Job.dateToString(j.getStartTime());
				my_data[6] = Job.timeToString(j.getStartTime());
				my_data[7] = Job.dateToString(j.getEndTime());
				my_data[8] = Job.timeToString(j.getEndTime());
				my_data[9] = j.getNumHeavyVolunteers() + "/" +  j.getTotalHeavyVolunteersNeeded();
				my_data[10] = j.getNumMedVolunteers() + "/" + j.getTotalMedVolunteersNeeded();
				my_data[11] = j.getNumLightVolunteers() + "/" + j.getTotalLightVolunteersNeeded();
				my_data[12] = j.getID();
				if (my_all) {
					my_data[13] =  isVolunteered(j);
				}
				my_model.addRow(my_data);				
			} catch (final UPException e) {
				System.out.println(e.getMessage());
			} catch (final ArrayIndexOutOfBoundsException aiobe) { //should never be reached
				System.out.println(aiobe.getMessage());
						
			}
		}
	}
	
	/**
	 * Checks whether the volunteer has already signed up
	 * for the job.
	 * 
	 * @param the_job to check.
	 * @return boolean whether the volunteer is signed up or not.
	 */
		
	private boolean isVolunteered(Job the_job) {
		boolean result = false;
		
		int user_id = my_lm.getCurrentUser().getID();
		
		for (int i : the_job.getVolunteerIDs()) {
			if (i == user_id) {
				result = true;
				break;
			}
		}
		
		return result;
	}

}
